package org.noote.libs.sogle;

import javax.microedition.khronos.opengles.GL10;

public class Material {
	
	protected float matAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	protected float matDiffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
	protected float matSpecular[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	protected float matSpecularFactor = 0;
	protected float matEmission[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	
	public void setAmbient(float red, float green, float blue)
	{
		setAmbient(red, green, blue, 1);
	}
	public void setAmbient(float red, float green, float blue, float alpha)
	{
		matAmbient[0] = red;
		matAmbient[1] = green;
		matAmbient[2] = blue;
		matAmbient[3] = alpha;
	}
	public void noAmbient()
	{
		setAmbient(0, 0, 0, 1);
	}
	
	public void setDiffuse(float red, float green, float blue)
	{
		setDiffuse(red, green, blue, 1);
	}
	public void setDiffuse(float red, float green, float blue, float alpha)
	{
		matDiffuse[0] = red;
		matDiffuse[1] = green;
		matDiffuse[2] = blue;
		matDiffuse[3] = alpha;
	}
	
	public void setSpecular(float factor, float red, float green, float blue)
	{
		setSpecular(factor, red, green, blue, 1);
	}
	public void setSpecular(float factor, float red, float green, float blue, float alpha)
	{
		matSpecularFactor = factor>128?128:factor;
		matSpecular[0] = red;
		matSpecular[1] = green;
		matSpecular[2] = blue;
		matSpecular[3] = alpha;
	}
	public void noSpecular()
	{
		setSpecular(0, 0, 0, 0, 1);
	}
	
	public void setEmission(float red, float green, float blue)
	{
		setEmission(red, green, blue, 1);
	}
	public void setEmission(float red, float green, float blue, float alpha)
	{
		matEmission[0] = red;
		matEmission[1] = green;
		matEmission[2] = blue;
		matEmission[3] = alpha;
	}
	public void noEmission()
	{
		setEmission(0, 0, 0, 1);
	}
	
	public void set(Material mat)
	{
		this.matAmbient = mat.matAmbient;
		this.matDiffuse = mat.matDiffuse;
		this.matSpecular = mat.matSpecular;
		this.matSpecularFactor = mat.matSpecularFactor;
		this.matEmission = mat.matEmission;
	}
	
	protected void draw(GL10 gl)
	{
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, matSpecular, 0);
        gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, matSpecularFactor);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, matEmission, 0);
	}
}
